﻿using Abp.Application.Services.Dto;
using Abp.Authorization;
using Microsoft.AspNetCore.Mvc;
using shenghua.Authorization;
using shenghua.Basics.CommoditySpecificationParameters;
using shenghua.Basics.CommodityWares;
using shenghua.Basics.CommodityWareSetting.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Basics.CommodityWareSetting
{
    [AbpAuthorize(PermissionNames.CommodityWareManagement)]
    public class CommodityWareAppService: shenghuaAppServiceBase, ICommodityWareAppService
    {
        private readonly CommodityWareManager _commodityWareManager;
        private readonly CommoditySpecificationParameterManager _commoditySpecificationParameter;

        public CommodityWareAppService(
            CommodityWareManager commodityWareManager,
            CommoditySpecificationParameterManager commoditySpecificationParameter)
        {
            _commoditySpecificationParameter = commoditySpecificationParameter;
            _commodityWareManager = commodityWareManager;
        }

        public async Task<CommodityWareDto> CreateWare(CreateCommodityWareDto input)
        {
            CommodityWare commodityWare = new CommodityWare
            {
                Name = input.Name,
                DisplayName = input.DisplayName
            };
            await _commodityWareManager.Add(commodityWare);
            return ObjectMapper.Map<CommodityWareDto>(commodityWare);
        }

        public async Task<CommodityWareDto> UpdateWare(UpdateCommodityWareDto input)
        {
            CommodityWare commodityWare = await _commodityWareManager.Get(input.Id);
            commodityWare.DisplayName = input.DisplayName;
            await _commodityWareManager.Update(commodityWare);
            return ObjectMapper.Map<CommodityWareDto>(commodityWare);
        }

        public async Task SortUpWare(EntityDto input)
        {
            await _commodityWareManager.SortUp(input.Id);
        }

        public async Task SortDownWare(EntityDto input)
        {
            await _commodityWareManager.SortDown(input.Id);
        }

        [HttpPost]
        public async Task DeleteWare(EntityDto input)
        {
            await _commodityWareManager.Delete(input.Id);
        }

        public async Task<List<CommodityWareDto>> GetAllWares()
        {
            List<CommodityWare> commodityWares = await _commodityWareManager.GetAll();
            return ObjectMapper.Map<List<CommodityWareDto>>(commodityWares);
        }

        public async Task<CommoditySpecificationParameterDto> CreateSpecificationParameter(CreateCommoditySpecificationParameterDto input)
        {
            CommoditySpecificationParameter commoditySpecificationParameter = new CommoditySpecificationParameter
            {
                CommodityWareId = input.CommodityWareId,
                Name = input.Name,
                DisplayName = input.DisplayName,
                Enabled = input.Enabled
            };
            await _commoditySpecificationParameter.Add(commoditySpecificationParameter);
            return ObjectMapper.Map<CommoditySpecificationParameterDto>(commoditySpecificationParameter);
        }

        public async Task<CommoditySpecificationParameterDto> UpdateSpecificationParameter(UpdateCommoditySpecificationParameterDto input)
        {
            CommoditySpecificationParameter commoditySpecificationParameter = await _commoditySpecificationParameter.Get(input.Id);
            commoditySpecificationParameter.DisplayName = input.DisplayName;
            commoditySpecificationParameter.Enabled = input.Enabled;
            await _commoditySpecificationParameter.Update(commoditySpecificationParameter);
            return ObjectMapper.Map<CommoditySpecificationParameterDto>(commoditySpecificationParameter);
        }

        public async Task SortUpSpecificationParameter(EntityDto input)
        {
            await _commoditySpecificationParameter.SortUp(input.Id);
        }

        public async Task SortDownSpecificationParameter(EntityDto input)
        {
            await _commoditySpecificationParameter.SortDown(input.Id);
        }

        [HttpPost]
        public async Task DeleteSpecificationParameter(EntityDto input)
        {
            await _commoditySpecificationParameter.Delete(input.Id);
        }

        public async Task<List<CommoditySpecificationParameterDto>> GetAllSpecificationParameters(int commodityWareId)
        {
            List<CommoditySpecificationParameter> commoditySpecificationParameters =
                await _commoditySpecificationParameter.GetAll(commodityWareId);
            return ObjectMapper.Map<List<CommoditySpecificationParameterDto>>(commoditySpecificationParameters);
        }
    }
}
